package com.samehope.ar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.dto.ProblemTypeAddParam;
import com.samehope.ar.dto.ProblemTypeUpdateParam;
import com.samehope.ar.entity.Problem;
import com.samehope.ar.entity.ProblemType;
import com.samehope.ar.mapper.ProblemTypeMapper;
import com.samehope.ar.service.IProblemService;
import com.samehope.ar.service.IProblemTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.vo.FormSelectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 问题分类表 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2020-01-06
 */
@Service
public class ProblemTypeServiceImpl extends ServiceImpl<ProblemTypeMapper, ProblemType> implements IProblemTypeService {

    @Autowired
    private IProblemService problemService;

    /**
     * 删除问题分类
     * @param problemTypeId
     */
    @Override
    public void deleteProblemType(Long problemTypeId) {

        ProblemType problemType = this.baseMapper.findByProjectAndProblemType(SessionUtil.getProjectId(), problemTypeId);

        if (problemType == null) {
            throw new IllegalArgumentException("问题分类不存在");
        }

        QueryWrapper<Problem> qw = new QueryWrapper<>();
        qw.eq("project_id", SessionUtil.getProjectId());
        qw.eq("problem_type_id", problemTypeId);
        qw.eq("del_flag", DelFlagEnum.NORMAL.getCode());
        int count = problemService.count(qw);
        if (count > 0) {
            throw new IllegalArgumentException("问题分类下存在问题, 无法删除");
        }

        problemType.setDelFlag(DelFlagEnum.DELETED.getCode());
        problemType.setUpdateBy(SessionUtil.getUserId());
        problemType.setUpdateDate(LocalDateTime.now());

        this.updateById(problemType);
    }

    /**
     * 添加问题分类
     * @param param
     */
    @Override
    public void add(ProblemTypeAddParam param) {

        ProblemType problemType = this.baseMapper.findByProjectAndName(SessionUtil.getProjectId(), param.getName());

        if (problemType != null) {
            throw new IllegalArgumentException("问题分类名称已存在");
        }

        problemType = new ProblemType();
        problemType.setCompanyId(SessionUtil.getCompanyId());
        problemType.setProjectId(SessionUtil.getProjectId());
        problemType.setName(param.getName());
        problemType.setCreateBy(SessionUtil.getUserId());

        this.save(problemType);
    }

    /**
     * 更新问题分类
     * @param param
     */
    @Override
    public void update(ProblemTypeUpdateParam param) {

        ProblemType src = this.baseMapper.findByProjectAndProblemType(SessionUtil.getProjectId(), param.getProblemTypeId());

        if (src == null) {
            throw new IllegalArgumentException("问题分类不存在");
        }


        ProblemType problemType = this.baseMapper.findByProjectAndName(SessionUtil.getProjectId(), param.getName());

        if (problemType != null && !src.getId().equals(problemType.getId())) {
            throw new IllegalArgumentException("问题分类名称已存在");
        }

        src.setName(param.getName());
        src.setUpdateBy(SessionUtil.getUserId());
        src.setUpdateDate(LocalDateTime.now());

        this.updateById(src);
    }

    /**
     * 问题分类下拉选的视图对象结合
     * @return
     */
    @Override
    public List<FormSelectVO> typesForSelect() {
        return this.baseMapper.typesForSelectByProject(SessionUtil.getProjectId());
    }
}
